import 'package:flutter/material.dart';
import 'package:ileddemo/code/ui/base/BaseWidget.dart';
import 'package:ileddemo/code/ui/base/TitleBarWidget.dart';
import 'package:lib_music/lib_mic.dart';
import 'package:lib_music/lib_music.dart';
import 'package:lib_music/listener/OnMusicCallback.dart';

import '../../../generated/l10n.dart';
import '../../util/PermissionUtil.dart';
import 'CtrlBarWidget.dart';
import 'MusicDataSendUtil.dart';

class MusicPage extends StatefulWidget {
  const MusicPage({super.key});

  @override
  State<MusicPage> createState() => _MusicPageState();
}

class _MusicPageState extends State<MusicPage> {
  bool isMicMode = false;
  double progress = -1;

  @override
  void initState() {
    LibMusic.instance.getMusicList();
    MusicDataSendUtil.instance;
    super.initState();
  }

  @override
  void dispose() {
    LibMusic.instance.stopPlay();
    LibMic.instance.stopRecord();
    super.dispose();
  }

  String formatMillisecondsToTime(int milliseconds) {
    final seconds = (milliseconds / 1000).floor().remainder(60);
    final minutes = (milliseconds / (1000 * 60)).floor();

    final minuteString = minutes.toString();
    final secondString = seconds.toString().padLeft(2, '0');

    return '$minuteString:$secondString';
  }

  @override
  Widget build(BuildContext context) {
    return BaseWidget(
      child: ListenableBuilder(
        listenable: MusicDataSendUtil.instance,
        builder: (BuildContext context, Widget? child) {
          var musicManager = LibMusic.instance;
          var bean = musicManager.getCurrentMusic();
          //艺术家
          String artist = "unknown";
          //名字
          String title = "unknown";
          //地址
          String? url;
          //时长
          int duration = 0;
          if (bean != null) {
            if (bean.title != null) {
              title = bean.title!;
            }
            if (bean.artist != null) {
              artist = bean.artist!;
            }
            if (bean.url != null) {
              url = bean.url!;
            }
            if (bean.duration != null) {
              duration = bean.duration!;
            }
          }

          return Column(
            children: [
              TitleBarWidget(title: S.of(context).str_id_25),

              Expanded(child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [

                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    ModeBtn(
                      isSelected: MusicDataSendUtil.instance.effects == 0,
                      title: S.of(context).str_id_30,
                      onTap: () {
                        MusicDataSendUtil.instance.effects = 0;
                      },
                    ),
                    ModeBtn(
                      isSelected: MusicDataSendUtil.instance.effects == 1,
                      title: S.of(context).str_id_31,
                      onTap: () {
                        MusicDataSendUtil.instance.effects = 1;
                      },
                    ),
                    ModeBtn(
                      isSelected: MusicDataSendUtil.instance.effects == 2,
                      title: S.of(context).str_id_32,
                      onTap: () {
                        MusicDataSendUtil.instance.effects = 2;
                      },
                    ),
                  ],
                ),
                const SizedBox(height: 120),
                Text(title, style: const TextStyle(color: Colors.black, fontSize: 16)),
                Text(artist, style: const TextStyle(color: Colors.black, fontSize: 14)),

                //播放进度条
                Row(
                  children: [
                    const SizedBox(width: 10),
                    Text(
                      formatMillisecondsToTime(musicManager.currentProgress),
                      style: const TextStyle(fontSize: 14, color: Colors.grey),
                    ),
                    Expanded(
                      child: Slider(
                        value: progress == -1 ? musicManager.currentProgress.toDouble() : progress,
                        min: 0,
                        max: duration.toDouble(),
                        onChanged: (double newValue) {
                          if (musicManager.currentProgress > 0) {
                            setState(() {
                              progress = newValue;
                            });
                          }
                        },
                        onChangeEnd: (double newValue) {
                          if (musicManager.currentProgress > 0) {
                            progress = -1;
                            musicManager.seekTo(newValue.toInt());
                          }
                        },
                      ),
                    ),
                    Text(formatMillisecondsToTime(duration), style: const TextStyle(fontSize: 14, color: Colors.grey)),
                    const SizedBox(width: 10),
                  ],
                ),
                const SizedBox(height: 15),
                //控制条
                const CtrlBarWidget(),
                const SizedBox(height: 120),

              ],))
            ],
          );
        },
      ),
    );
  }
}

class ModeBtn extends StatelessWidget {
  final String title;
  final bool isSelected;
  final GestureTapCallback? onTap;

  const ModeBtn({super.key, required this.title, this.onTap, required this.isSelected});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: onTap,
      child: Container(
        decoration: BoxDecoration(
          color: isSelected ? Colors.blueGrey : Colors.blueGrey.withAlpha(100),
          borderRadius: BorderRadius.circular(10),
        ),
        padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
        child: Text(title, style: TextStyle(color: Colors.black, fontSize: 12)),
      ),
    );
  }
}
